//
//  TSProfitResultTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/15.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

/** 通知名 */
public let TSCountDownFinishedNotification = Notification.Name("TSCountDownFinishedNotification")

class TSProfitResultTableViewCell: UITableViewCell {

    private var periodLabel : UILabel!
    private var endLabel : UILabel!
    private var endTimeLabel : UILabel!
    private var collectionView : UICollectionView!
    private var dataSource : [TSResultModel] = []
    private var model : TSMainPageModel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configViews()
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.countDownNotification), name: .OYCountDownNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.countDownFinishedNotification), name: TSCountDownFinishedNotification, object: nil)
    }
    
    @objc private func countDownFinishedNotification() {

         self.model!.countdownFinished = true
        
         endTimeLabel.text = "开奖中"
    }
    
    @objc private func countDownNotification() {
            
            // 计算倒计时
            let timeInterval = OYCountDownManager.sharedManager.timeIntervalWithIdentifier(identifier: self.model!.identifier)
            let countDown = self.model!.interval - timeInterval
            if !self.model!.countdownFinished {
                endTimeLabel.text = String(format: "%02d:%02d:%02d", countDown/3600, (countDown/60)%60, countDown%60)
            }
        }
    
    func setupCell(model:TSMainPageModel) {
        
        self.model = model
        
        periodLabel.text = "第\(model.newest?.issue ?? 0)期开奖号码"
        endLabel.text = "\(model.current)封盘时间"
        if self.model!.countdownFinished {
            endTimeLabel.text = "开奖中"
        }

        let numArray = model.newest?.code?.components(separatedBy: ",")
        if numArray?.count ?? 0 > 0 {
            for index in 0..<(numArray!.count) {
                let model = TSResultModel.init(title: Int(numArray![index])!, isSelected: true)
                dataSource.append(model)
            }
        }
        collectionView.reloadData()
    }
    
    @objc private func tapGesture(gesture:UITapGestureRecognizer) {
        
        let vc = TSDetailRecordViewController()
        vc.type = self.model.title
        UIViewController.currentViewController().navigationController?.pushViewController(vc, animated: true)
    }
    
    required init?(coder: NSCoder) {
        
        super.init(coder: coder)
        NotificationCenter.default.addObserver(self, selector: #selector(self.countDownNotification), name: .OYCountDownNotification, object: nil)
        
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension TSProfitResultTableViewCell : UICollectionViewDelegate {
    
}

extension TSProfitResultTableViewCell : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(TSResultCollectionViewCell.self), for: indexPath) as? TSResultCollectionViewCell
        
        cell?.setupCell(model: dataSource[indexPath.item])
        
        cell?.contentView.backgroundColor = BASE_PURPLE_COLOR
        
        return cell!
    }
}

extension TSProfitResultTableViewCell {
    
    private func configViews() {
        
        let topContainer = UIView.init()
        topContainer.backgroundColor = WHITE_COLOR
        topContainer.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapGesture(gesture:))))
        contentView.addSubview(topContainer)
        topContainer.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(30)
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#E6E6E6")
        topContainer.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "最新开奖结果"
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        topContainer.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
        }
        
        let icon = UIImageView.init(image: UIImage(named: "general_more"))
        topContainer.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(15)
            make.size.equalTo(CGSize(width: 8, height: 14))
        }
        
        let recentLabel = UILabel.init()
        recentLabel.text = "近期开奖"
        recentLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        recentLabel.font = UIFont.systemFont(ofSize: 14)
        topContainer.addSubview(recentLabel)
        recentLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(icon.snp_left).offset(-5)
        }
        
        periodLabel = UILabel.init()
        periodLabel.text = "第0期开奖号码"
        periodLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        periodLabel.font = UIFont.systemFont(ofSize: 10, weight: .medium)
        contentView.addSubview(periodLabel)
        periodLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(topContainer.snp_bottom).offset(10)
        }
        
        let itemW : CGFloat = 20
        let collectionW = CGFloat(SCREEN_WIDTH * 0.72)
        let margin = (collectionW - 11 * itemW) / 10
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: 20, height: 20)
        layout.minimumInteritemSpacing = margin
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = WHITE_COLOR
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.bounces = false
        collectionView.register(TSResultCollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(TSResultCollectionViewCell.self))
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(periodLabel.snp_bottom).offset(15)
            make.size.equalTo(CGSize(width: collectionW, height: 20))
        }
        
        endLabel = UILabel.init()
        endLabel.text = "0封盘时间"
        endLabel.textColor = UIColor.black
        endLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        contentView.addSubview(endLabel)
        endLabel.snp.makeConstraints { (make) in
            make.left.equalTo(periodLabel)
            make.top.equalTo(collectionView.snp_bottom).offset(14)
            
            make.bottom.equalTo(-10)
        }
        
        endTimeLabel = UILabel.init()
        endTimeLabel.text = "00:00:00"
        endTimeLabel.textColor = BASE_RED_COLOR
        endTimeLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        contentView.addSubview(endTimeLabel)
        endTimeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(endLabel.snp_right).offset(5)
            make.centerY.equalTo(endLabel)
        }
        
    }
}
